<?php

// Interface.php
// Michael Baker
// 2010.07.13
//
// provides functionality used by the admin interface

if ( !defined( "IN_MDM" ) )
  exit;
  
function ui_get_admin_menu( $user_id )
{
  // Get admin menu for a user ID
  
  global $dbase;
  $nav = array();
  
  $query  = "SELECT `id`, `is_cat`, `title`, `href`, ";
  $query .= "`order_id`, `flag_name`, `parent` ";
  $query .= "FROM WebAdminNav ORDER BY is_cat DESC, order_id ASC";
  $dbase->execute($query);
  $resultset = $dbase->fetchall();
  $numrows = $dbase->numrows();

  for ( $ctr = 0; $ctr < $numrows; $ctr++ )
  {
    $row = $resultset[ $ctr ];
    list($id, $is_cat, $title, $href, $order_id, $flag, $parent) = $row;

    // only add links that the user has access to
    if ( (!$is_cat) && (!has_flag($user_id, $flag)) )
    {
      $ctr++;
      continue;
    }
    
    if ( $is_cat )
    {
      // create array for this category
      $nav[ $id ] = array( $title, array() );
    }
    else
    {
      // Push this child into its parent's array in $nav
      $link = array( $title, $href );
      $nav[ $parent ][ 1 ][] = $link;
    }
    
  }
  
  // remove any category with no children visible to the user
  foreach ( $nav as $id => $cat_arr )
  {
    $children = $cat_arr[1];
    if ( !count( $children ) )
      unset( $nav[ $id ] );
  }
  
  return $nav;
}

function ui_get_node_option( $node_obj )
{
  // generate one <option> from a node object
  
  $ttl = $node_obj->get_title();
  $id  = $node_obj->get_id();
  $adr = $node_obj->get_address();
  
  return "<option value=\"$id\">$ttl [$adr]</option>";
}

function ui_get_node_options( $selected_id = false )
{
  // generate a string of <option>s from the database node interface
  
  global $dbase;
  
  $nodes = $dbase->list_nodes();
  $out = "";
  
  if ( $selected_id && array_key_exists( $selected_id, $nodes ) )
  {
    $out .= ui_get_node_option( $nodes[ $selected_id ] ) . "\n";
    unset( $nodes[ $selected_id ] );
  }
  
  foreach ( $nodes as $id => $node_obj )
    $out .= ui_get_node_option( $node_obj ) . "\n";
  
  return $out;
}

function ui_create_workflow( $ip_arr )
{
  global $dbase;
  
  if ( count( array_unique($ip_arr) ) < count( $ip_arr ) )
  {
    $str = "Duplicate nodes found in workflow";
    throw new Exception( $str );
  }
  
  // Convert {a, b, c, d} to {a, b, b, c, c, d} form
  $ip_arr2 = induce_duplicates( $ip_arr );
  
  // Check if it already exists
  if ( $dbase->workflow_exists_by_addr( $ip_arr2 ) )
    throw new Exception( "Workflow already <a href=\"wf_view.php?id=$exists\">exists</a>" );
  
  return $dbase->create_workflow( $ip_arr2 );
}

function ui_error( $msg )
{
  return "<div class=\"errorbox\"><strong>Error</strong><br /><br />$msg</div>";
}

function ui_info( $msg )
{
  return "<div class=\"successbox\"><strong>Information</strong><br /><br />$msg</div>";
}

function ui_create_link( $title, $href, $additional = "" )
{
  $append = $additional ? " $additional" : "";
  return "<a href=\"$href\"" . $append . ">$title</a>";
}

?>